package com.dz.utils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class OssUtil {


    /**
     * 文件上传
     */

   /* public static String uploadOss(MultipartFile multipartFile, String filePath) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.autoRegion());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        //...其他参数参考类注释

        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传

        //默认不指定key的情况下，以文件内容的hash值作为文件名

        OssProperties properties = new OssProperties();

        try {
            InputStream inputStream = multipartFile.getInputStream();
            // ByteArrayInputStream byteInputStream = new ByteArrayInputStream(uploadBytes);
            Auth auth = Auth.create(properties.getAccessKey(), properties.getSecretKey());
            String upToken = auth.uploadToken(properties.getBucket());
            try {
                Response response = uploadManager.put(inputStream, filePath, upToken, null, null);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
                return "http://s62n65xrd.hn-bkt.clouddn.com/" + filePath;
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    ex2.printStackTrace();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return "error"; // 返回外链地址
    }*/

}
